package njust.course;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import njust.hitcourse.HitCourse;
import njust.user.Login;
import njust.user.UserDAO;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CourseController extends HttpServlet {
    public CourseController(){
        super();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String courseFormCheck=req.getParameter("courseFormCheck");
        HttpSession httpSession= req.getSession();
        //System.out.println("courseFormCheck="+courseFormCheck);
        if (courseFormCheck.equals("addCourse")){
            //执行添加课程代码
            String courseType;
            if(req.getParameter("courseType").equals("b"))
                courseType="必修";
            else
                courseType="选修";
            if (req.getParameter("courseId").equals("")||req.getParameter("courseName").equals("")
                ||req.getParameter("courseNum").equals("")||req.getParameter("courseType").equals("")
            ){
                req.setAttribute("error_code",4);
                req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
                return;
            }
            Course course=new Course(Integer.parseInt(req.getParameter("courseId")),req.getParameter("courseName"),
                    Integer.parseInt(req.getParameter("courseNum")),req.getParameter("courseType"));
            if(course!=null){
                CourseService courseService=new CourseService();
                CourseDAO courseDAO=new CourseDAO();
                if(courseDAO.findCourseById(course.getCourseId())!=null){
                    //查找到了数据库中已经存在course
                    req.setAttribute("error_code",3);
                    req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
                    return;
                }
                courseService.insertCourseToDataBase(course);
                req.getRequestDispatcher("allCourse.jsp").forward(req,resp);
            }
        }
        else if (courseFormCheck.equals("delCourse")){
            //执行删除课程代码
            int count=0;
            String[] courseIds = req.getParameterValues("courseId");
            if (courseIds==null){
                req.setAttribute("error_code",1);   //结果集为空
                req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
                return;
            }
            for(String s:courseIds){
                Course course=new Course(Integer.parseInt(s));
                CourseService courseService=new CourseService();
                courseService.deleteCourseToDataBase(course);
                count++;
            }
            if(count>0){
                //删除成功
                req.setAttribute("message","删除成功");
                req.getRequestDispatcher("delCourse.jsp").forward(req,resp);
            }
            else{
                req.setAttribute("error_code",2);   //没有选中任何课程
                req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
            }
        }
        else if(courseFormCheck.equals("chooseCourse")){
            //在allCourse.jsp页面上选择想要的课程
            //待选课程打钩
            int count=0;
            String[] courseIds = req.getParameterValues("courseId");
            if (courseIds==null){
                req.setAttribute("error_code",6);   //结果集为空
                req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
                return;
            }
            Login userinfo = (Login) httpSession.getAttribute("userinfo");
            UserDAO userDAO=new UserDAO();
            for(String s:courseIds){
                Course course=new Course(Integer.parseInt(s));
                CourseService courseService=new CourseService();
                if (courseService.isFindCourse(userDAO.getUserId(userinfo),course.getCourseId())){
                    req.setAttribute("error_code",7);  //重复选择课程
                    req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
                    return;
                }
                courseService.addHitCourseToDataBase(userDAO.getUserId(userinfo),course.getCourseId(),0);
                count++;
            }
            if(count>0){
                req.getRequestDispatcher("hitCourse.jsp").forward(req,resp);
            }
            else{
                req.setAttribute("error_code",2);   //没有选中任何课程
                req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
            }

        }
        else if(courseFormCheck.equals("withDrawCourse")){
            //在hitCourse.jsp页面上删除退选的课程
            //退选课程打钩
            int count=0;
            String[] courseIds = req.getParameterValues("hitCourseId");
            if (courseIds==null){
                req.setAttribute("error_code",5);   //结果集为空
                req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
                return;
            }
            Login userinfo = (Login) httpSession.getAttribute("userinfo");
            UserDAO userDAO=new UserDAO();
            for(String s:courseIds){
                Course course=new Course(Integer.parseInt(s));
                CourseService courseService=new CourseService();
                courseService.withDrawCourse(userDAO.getUserId(userinfo),course.getCourseId());
                courseService.lowCourseStudentNum(course.getCourseId());
                count++;
            }
            if(count>0){
                //删除成功
                req.getRequestDispatcher("hitCourse.jsp").forward(req,resp);
            }
            else{
                req.setAttribute("error_code",2);   //没有选中任何课程
                req.getRequestDispatcher("courseFailure.jsp").forward(req,resp);
            }

        }

        else if (courseFormCheck.equals("prePage")){

            System.out.println("执行跳转到上一页代码");
            int pageNum = (int) httpSession.getAttribute("pageNum");
            pageNum--;
            httpSession.setAttribute("pageNum",pageNum);
            req.getRequestDispatcher("allCourse.jsp").forward(req,resp);

        }else if(courseFormCheck.equals("nextPage")){

            //System.out.println("执行跳转到下一页代码");
            int pageNum = (int) httpSession.getAttribute("pageNum");
            pageNum++;
            httpSession.setAttribute("pageNum",pageNum);
            req.getRequestDispatcher("allCourse.jsp").forward(req,resp);
        }else if(courseFormCheck.equals("changeOrder")){

            System.out.println("排序代码");
            turnFlag();
            req.setAttribute("orderPage",flagOrderPage);
            req.getRequestDispatcher("allCourse.jsp").forward(req,resp);
        }else if(courseFormCheck.contains("changePage")){
            System.out.println("具体页码跳转代码");
            char last=courseFormCheck.charAt(courseFormCheck.length()-1);
            int pageNum=last-'0';
            httpSession.setAttribute("pageNum",pageNum-1);
            req.getRequestDispatcher("allCourse.jsp").forward(req,resp);
        }

        else {
            System.out.println("未知提交错误");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
    //0表示降序，1表示升序
    private int flagOrderPage=0;
    protected void turnFlag(){
        if (flagOrderPage==0)
            flagOrderPage=1;
        else if (flagOrderPage==1)
            flagOrderPage=0;
        else
            System.out.println("flagOrderPage出现未预料的值");

    }
}
